package com.smallkingson.ylib.view;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

public class YRadioGroup extends RelativeLayout implements OnClickListener, OnCheckedChangeListener {

	private Context mContext;

	private List<android.widget.CompoundButton> group;

	private OnRaidoClickListener onRaidoClickListener;

	private OnItemCheckedChangedListener onItemCheckedChangedListener;

	public void setOnItemCheckedChangedListener(OnItemCheckedChangedListener onItemCheckedChangedListener) {

		this.onItemCheckedChangedListener = onItemCheckedChangedListener;
	}

	public void setOnRaidoClickListener(OnRaidoClickListener onRaidoClickListener) {

		this.onRaidoClickListener = onRaidoClickListener;
	}
	public YRadioGroup(Context context) {

		super(context);

		init(context);
	}

	public YRadioGroup(Context context, AttributeSet attrs) {

		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		mContext = context;

		group = new ArrayList<android.widget.CompoundButton>();
	}
	@Override
	protected void onAttachedToWindow() {

		// TODO Auto-generated method stub
		super.onAttachedToWindow();

		initGroup();
	}

	@Override
	public void addView(View child) {

		super.addView(child);

		initGroup();
	}

	private void initGroup() {

		this.group.clear();

		addAllRadio(this);

	}

	void addAllRadio(ViewGroup vg) {

		for (int i = 0; i < vg.getChildCount(); i++) {

			View child = vg.getChildAt(i);

			if (child instanceof ViewGroup) {

				addAllRadio((ViewGroup) child);

			} else if (child instanceof android.widget.CompoundButton) {

				android.widget.CompoundButton rb = (android.widget.CompoundButton) child;

				rb.setOnClickListener(this);
				rb.setOnCheckedChangeListener(this);
				group.add(rb);
			}
		}
	}

	public void setChecked(int id) {
		for (android.widget.CompoundButton rb : group) {
			if (rb.getId() == id) {
				rb.setChecked(true);
			}else{
				rb.setChecked(false);
			}
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		if (onItemCheckedChangedListener != null) {
			onItemCheckedChangedListener.onItemCheckedChanged(buttonView, isChecked);
		}
	}

	@Override
	public void onClick(View v) {

		android.widget.CompoundButton btn = (android.widget.CompoundButton) v;

		for (android.widget.CompoundButton r : group) {

			r.setChecked(false);
		}

		btn.setChecked(true);

		if (onRaidoClickListener != null) {

			onRaidoClickListener.onRaidoClick(v);
		}
	}

	/**
	 * 获取被选中的radio
	 * 
	 * @return
	 */
	public android.widget.CompoundButton getCheckedRadio() {

		for (android.widget.CompoundButton rb : group) {

			if (rb.isChecked()) {

				return rb;
			}
		}
		return null;
	}

	public interface OnItemCheckedChangedListener {

		public void onItemCheckedChanged(CompoundButton buttonView, boolean isChecked);
	}

	public interface OnRaidoClickListener {

		void onRaidoClick(View v);
	}

}
